package org.mspring.mlog.action.admin;

import org.mspring.mlog.entity.Config;
import org.mspring.mlog.entity.em.ConfigGroup;
import org.mspring.mlog.redis.ConfigRedisValueService;
import org.mspring.mlog.service.ConfigService;
import org.mspring.nbee.common.utils.RequestUtils;
import org.mspring.nbee.web.Action;
import org.mspring.nbee.web.support.WebContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author Gao Youbo
 * @since 2015/3/29.
 */
@Action
@RequestMapping("/admin/redis")
public class RedisAction {
    private ConfigRedisValueService configRedisValueService;
    private ConfigService configService;

    @RequestMapping("/put")
    public void put(WebContext context) {
        Config config = configService.findByName(ConfigGroup.BASE, "sitename");
        configRedisValueService.put("sitename", config);
        context.renderJsonResult(true);
    }

    @RequestMapping("/get")
    public void get(WebContext context) {
        String key = RequestUtils.getParam(context.getRequest(), "key", "sitename");
        Config value = configRedisValueService.get(key);
        context.renderJsonResult(value);
    }

    @RequestMapping("/evict")
    public void evict(WebContext context) {
        String key = RequestUtils.getParam(context.getRequest(), "key", "sitename");
        configRedisValueService.evict(key);
        context.renderJsonResult(true);
    }

    @Autowired
    public void setConfigRedisValueService(ConfigRedisValueService configRedisValueService) {
        this.configRedisValueService = configRedisValueService;
    }

    @Autowired
    public void setConfigService(ConfigService configService) {
        this.configService = configService;
    }
}
